package com.carcontrol.rotate;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import com.carcontrol.rotate.base.BaseFragment;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

//天窗
public class Fragment_Fourth extends BaseFragment {
    public static String TAG = "4";

    private FrameLayout v;
    private Context context;

    GifDrawable gifDrawable_on;
    GifDrawable gifDrawable_off;
    GifDrawable gifDrawable2_on;
    GifDrawable gifDrawable2_off;

    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;
    Thread thread;

    Object[] allParam = new Object[]{

//            new Object[]{"iv", new Rect(373*width/width0-width_d, 151*height/height0-height_d, 777*width/width0, 285*height/height0), R.mipmap.border, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(631*width/width0-width_d, 190*height/height0-height_d, 256*width/width0, 89*height/height0), R.mipmap.scuttle, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

//            //天窗开
//            new Object[]{"iv", new Rect(460*width/width0-width_d, 328*height/height0-height_d, 103*width/width0, 103*height/height0), R.mipmap.open_off, true, "9110", R.mipmap.open_on},
//            //天窗关
//            new Object[]{"iv", new Rect(572*width/width0-width_d, 328*height/height0-height_d, 103*width/width0, 103*height/height0), R.mipmap.close_off, true, "9210", R.mipmap.close_on},
            //天窗帘开
            new Object[]{"iv", new Rect(497*width/width0-width_d, 328*height/height0-height_d, 103*width/width0, 103*height/height0), R.mipmap.open_l_off, true, "9310", R.mipmap.open_l_on},
            //天窗帘关
            new Object[]{"iv", new Rect(869*width/width0-width_d, 328*height/height0-height_d, 103*width/width0, 103*height/height0), R.mipmap.close_l_off, true, "9410", R.mipmap.close_l_on},

//            //天窗文字
//            new Object[]{"iv", new Rect(368*width/width0-width_d, 363*height/height0-height_d, 79*width/width0, 32*height/height0), R.mipmap.txt_scuttle, false, "", R.mipmap.close_l_off},
//            //天窗帘文字
//            new Object[]{"iv", new Rect(778*width/width0-width_d, 363*height/height0-height_d, 113*width/width0, 32*height/height0), R.mipmap.txt_scuttle2, false, "", R.mipmap.close_l_off},

//            new Object[]{"tv", new Rect(410*width/width0-width_d, 333*height/height0-height_d, 0, 0), "天窗", 18, Color.WHITE},
//            new Object[]{"tv", new Rect(783*width/width0-width_d, 333*height/height0-height_d, 0, 0), "天窗帘", 18, Color.WHITE},

//            //天窗动画
//            new Object[]{"gif", new Rect(378*width/width0-width_d, 158*height/height0-height_d, 286*width/width0, 154*height/height0), R.mipmap.scuttle_on, false, R.mipmap.scuttle_off},
            //天窗帘动画
            new Object[]{"gif", new Rect(590*width/width0-width_d, 160*height/height0-height_d, 289*width/width0, 157*height/height0), R.mipmap.scuttle2_on_gif, false, R.mipmap.scuttle2_off_gif},

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());

        context = v.getContext();

        Log.e("fragment_Fourth===0", v+"==="+width_d+"==="+height_d);

        initView();


//        isThread = true;
//        loopRead();

        return v;
    }

    protected void doAction1(String myDevice, String myStatus){
        Log.e("f4===doAction1", myDevice+"==="+myStatus);

//        if(myDevice.equals("9001")){
//            if(myStatus.equals("0001")){  //TODO
//            }else{
//            }
//
//            gif.setImageResource(imgSrc_on);
//            gifDrawable_on.start();
//        }else if(myDevice.equals("9002")){
//            gif.setImageResource(imgSrc_off);
//            gifDrawable_on.start();
//        }else if(myStatus.equals("9003")){  //TODO
//            gif2.setImageResource(imgSrc2_on);
//            gifDrawable2_on.start();
//        }else if(myStatus.equals("9004")){
//            gif2.setImageResource(imgSrc2_off);
//            gifDrawable2_on.start();
//        }

//        if(myDevice.equals("d001")){
////            if(myStatus.equals("0001")){  //TODO
////                gif.setImageResource(imgSrc_on);
////                gifDrawable_on.start();
////            }else{
////                gif.setImageResource(imgSrc_off);
////                gifDrawable_on.start();
////            }
//
//            if(myStatus.equals("0001")){  //TODO
//                gif2.setImageResource(imgSrc2_on);
//                gifDrawable2_on.start();
//            }else{
//                gif2.setImageResource(imgSrc2_off);
//                gifDrawable2_on.start();
//            }
//        }

    }



    @Override
    public void onDestroy() {
        Log.e("f4===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    int imgSrc_on;
    int imgSrc2_on;
    int imgSrc_off;
    int imgSrc2_off;
    GifImageView gif;
    GifImageView gif2;
    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];
            Rect rect = (Rect)obj[1];
            String type = (String)obj[0];

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                v.addView(tv, params);
            }else if("gif".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
//                int imgSrc2 = (int)obj[4];
                GifImageView iv_route = new GifImageView(context);
//                iv_route.setImageResource(imgSrc);
//                iv_route.setScaleType(ImageView.ScaleType.FIT_XY);
                if(imgSrc==R.mipmap.scuttle_on){
                    imgSrc_on = imgSrc;

                    gif = new GifImageView(context);
                    gif.setImageResource(imgSrc);
                    gif.setScaleType(ImageView.ScaleType.FIT_XY);
                    gifDrawable_on = (GifDrawable) gif.getDrawable();
                    gifDrawable_on.stop();

                    imgSrc_off = (int)obj[4];
//                    GifImageView iv_route_off = new GifImageView(context);
//                    iv_route_off.setImageResource(imgSrc_off);
//                    iv_route_off.setScaleType(ImageView.ScaleType.FIT_XY);
//
//                    gifDrawable_off = (GifDrawable) iv_route_off.getDrawable();
//
//                    iv_route_off.setVisibility(View.GONE);
//                    v.addView(iv_route_off, params);

                    Log.e("fragment_Fourth=gif", imgSrc_off+"==="+gif+"==="+gifDrawable_on);

                    v.addView(gif, params);
                }else if(imgSrc==R.mipmap.scuttle_off){
                    gifDrawable_off = (GifDrawable) iv_route.getDrawable();
                    gifDrawable_off.stop();
                }else if(imgSrc==R.mipmap.scuttle2_on_gif){
                    imgSrc2_on = imgSrc;

                    gif2 = new GifImageView(context);
                    gif2.setImageResource(imgSrc);
                    gif2.setScaleType(ImageView.ScaleType.FIT_XY);
                    gifDrawable2_on = (GifDrawable) gif2.getDrawable();
                    gifDrawable2_on.stop();

                    imgSrc2_off = (int)obj[4];

                    v.addView(gif2, params);
                }else if(imgSrc==R.mipmap.scuttle2_off_gif){
                    gifDrawable2_off = (GifDrawable) iv_route.getDrawable();
                    gifDrawable2_off.stop();
                }

            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);

                boolean isCanClick = (boolean)obj[3];

//                Log.e("fragment_Fourth===can", imgSrc+"==="+isCanClick);

                if(isCanClick){
                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===fourth", event.getAction()+"==="+MainActivity.curPos);

                            if(MainActivity.curPos!=2) return false;

                            int imgId = (int)obj[2];
                            int imgId2 = (int)obj[5];
                            String cmd = (String)obj[4];

                            Log.e("onTouch===", v+"==="+event.getAction()+"==="+imgId+"==="+imgSrc_off+"==="+cmd);

                            if(event.getAction()==1){

                                iv.setImageResource(imgId);

                                if(imgId==R.mipmap.open_off){
//                                    gif.setImageResource(imgSrc_on);
//                                    gifDrawable_on.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("9110"));
//                                    uartJnit.getInstance().sendCmds(toCmd("D1E0"));

//                                    read(bytes); //TODO  轮询读状态

//                                    ((Object[])allParam[3])[3] = true;
//                                    gifObj[3] = true;
                                }else if(imgId==R.mipmap.close_off){
//                                    gifDrawable_on.stop();
//                                    iv_route_off.setVisibility(View.VISIBLE);

//                                    gif.setImageResource(imgSrc_off);
//                                    gifDrawable_on.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("9210"));


//                                    ((Object[])allParam[3])[3] = false;
//                                    gifObj[3] = false;
                                }else if(imgId==R.mipmap.open_l_off){
                                    gif2.setImageResource(R.mipmap.scuttle2_on_gif);
                                    gifDrawable2_on.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("9310"));
//                                    uartJnit.getInstance().sendCmds(toCmd("D1E0"));

//                                    ((Object[])allParam[3])[3] = true;
//                                    gifObj[3] = true;
                                }else if(imgId==R.mipmap.close_l_off){
                                    gif2.setImageResource(R.mipmap.scuttle2_off_gif);
                                    gifDrawable2_on.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("9410"));
//                                    uartJnit.getInstance().sendCmds(toCmd("D1E0"));

//                                    ((Object[])allParam[3])[3] = false;
//                                    gifObj[3] = false;
                                }

//                                uartJnit.getInstance().sendCmds(toCmd(cmd));
                                send(cmd);

                            }else if(event.getAction()==0){
//                                if(imgId==R.mipmap.open_off){
//                                    iv.setImageResource(R.mipmap.open_on);
//                                }else if(imgId==R.mipmap.close_off){
//                                    iv.setImageResource(R.mipmap.close_on);
//                                }else if(imgId==R.mipmap.open_l_off){
//                                    iv.setImageResource(R.mipmap.open_l_on);
//                                }else if(imgId==R.mipmap.close_l_off){
//                                    iv.setImageResource(R.mipmap.close_l_on);
//                                }

//                                if(imgId==R.mipmap.open_l_off){
//                                    gif2.setImageResource(imgSrc2_on);
//                                    gifDrawable2_on.start();
//
////                                    uartJnit.getInstance().sendCmds(toCmd("9310"));
////                                    uartJnit.getInstance().sendCmds(toCmd("D1E0"));
//
////                                    ((Object[])allParam[3])[3] = true;
////                                    gifObj[3] = true;
//                                }else if(imgId==R.mipmap.close_l_off){
//                                    gif2.setImageResource(imgSrc2_off);
//                                    gifDrawable2_on.start();
//
////                                    uartJnit.getInstance().sendCmds(toCmd("9410"));
////                                    uartJnit.getInstance().sendCmds(toCmd("D1E0"));
//
////                                    ((Object[])allParam[3])[3] = false;
////                                    gifObj[3] = false;
//                                }

//                                send(cmd2);

                                iv.setImageResource(imgId2);
                            }

//                            write(bytes); //TODO

                            return true;
                        }
                    });

//                    iv.setOnClickListener(new OnClickListener() {
//
//                        @Override
//                        public void onClick(View v) {
////                        ToastUtil.showMessageApp(context, imgSrc+"==="+obj[2]);
//
////                            int imgId = (int)obj[2];
////                            byte[] bytes = (byte[])obj[4];
////
////                            Log.e("fragment_Fourth===iv", imgId+"==="+bytes.length+"==="+bytes);
//
////                            if(imgId==R.mipmap.open){
////                                iv.setImageResource(R.mipmap.open2);
////                                obj[2] = R.mipmap.open2;
////
////                            }else if(imgId==R.mipmap.open2){
////                                iv.setImageResource(R.mipmap.open);
////                                obj[2] = R.mipmap.open;
////                            }else if(imgId==R.mipmap.close){
////                                iv.setImageResource(R.mipmap.close2);
////                                obj[2] = R.mipmap.close2;
////                            }else if(imgId==R.mipmap.close2){
////                                iv.setImageResource(R.mipmap.close);
////                                obj[2] = R.mipmap.close;
////                            }
//
//                            /* 串口发送字节 */
////                            write(bytes); //TODO
//
//
//                        }
//                    });
                }

            }



        }

    }

    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(MainActivity.curPos==3){
                        m_myHandler.sendEmptyMessage(1);
                    }


                }

            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO

                    Log.e("f4===", "===");

                    break;


                default:
                    break;
            }
            return false;
        }
    });

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

    }

}

